// 网关配置的前缀
var companyServer = "/companySer"

// let userId = "1354255569179971599"
let userId = ""

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量

var search_value = "";
let position_value = "";
let financingSituation = "";
var companySize = '-1,';   //  公司规模

let followedList = [];

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });

    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    // console.log(userId);
    getFollowedList()
    getFinancingSituation();
    getCompanySize();
    getCompanies(1, 10, search_value, financingSituation, companySize, position_value);
}

function getCompanies(currentPage, pageSize, search_value,
                      financingSituation, companySize, position_value) {

    $.ajax({
        type: "get",
        async: false,
        url: companyServer + "/company/companyInfo",
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
            companySize: companySize,
            financingSituation: financingSituation,
            companyAddress: position_value,
            companyStatus: "正常",
        },
        success: function (data) {
            console.log("所有的企业数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            $("#title").html(total);
            //清空数据
            $("#companyInfo1").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let id = item.companyId;
                let aixin = "";
                if ($.inArray(id, followedList) >= 0){
                    aixin = "<img src='/images/aixin-2.png' style='width: 20px;height: 20px;' />";
                }else {
                    aixin = "<i class='far fa-heart'></i>";
                }

                $("#companyInfo1").append(
                    "<div class='col-lg-12 mb-20'>" +
                    "<div class='single-employer-list style-two'>" +
                    "<div class='info-top align-items-start'>" +
                    "<div class='employer-image'>" +
                    "<a onclick='companyDetail(\"" + item.companyId + "\")'>" +
                    "<img src='" + item.logo + "' alt='企业 Logo' style='height: 80px; width: 80px;'>" +
                    "</a>" +
                    "</div>" +
                    "<div class='employer-info'>" +
                    "<div class='employer-info-inner'>" +
                    "<div class='employer-info-top'>" +
                    "<div class='saveJob for-listing'>" +
                    "<span class='featured-label mr-20'>精选</span>" +
                    "<a class='save-job' data-toggle='modal' disabled='disabled'>" +
                    aixin +
                    "</a>" +
                    "</div>" +
                    "<div class='title-name'>" +
                    "<h3 class='employer-title'>" +
                    "<a  onclick='companyDetail(\"" + item.companyId + "\")'>" + item.companyName + "</a>" +
                    "</h3>" +
                    "</div>" +
                    "</div>" +
                    "<div class='employer-meta'>" +
                    "<div class='field-map'>" +
                    "<i class='lnr lnr-map-marker'> " + item.companyAddress + "</i>" +
                    "</div>" +
                    "</div>" +
                    "<div class=field-description>" +
                    "<p>" + item.introduction.substring(0, 20) + " ……" + "</p>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>" +
                    "</div>"
                );

            });

            // pageMe.js 使用方法
            $("#page1").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getCompanies(num, size, search_value, financingSituation, companySize, position_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            // 博客名为空
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }

    });

}

// 获得 融资情况
function getFinancingSituation() {
    let $list = $("#financingSituation .list");
    let $mySelect = $("#financingSituation .mySelect");
    $.ajax({
        type: "get",
        async: false,
        url: companyServer + "/company/financingSituation",
        success: function (data) {
            console.log("融资情况");

            // 如果为空
            if (data.datas.length <= 0) {
                $mySelect.empty();
                $mySelect.append(
                    "<option>暂无数据</option>"
                    // "<li data-value='' class='option' disabled='disabled'>暂无数据</li>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                $list.append(
                    "<li data-value='" + item.value + "' class='option'>" + item.value + "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<option>暂无数据</option>"
            );
            console.log(err);
        }

    });
}

// 获得 融资情况
function getCompanySize() {
    let $companySize = $("#companySize");
    $.ajax({
        type: "get",
        async: false,
        url: companyServer + "/company/companySize",
        success: function (data) {
            console.log("公司规模");

            // 如果为空
            if (data.datas.length <= 0) {
                $companySize.empty();
                $companySize.append(
                    "<li>" +
                    "<div class='filter-name-item'>" +
                    "<input type='checkbox' value='' name='company-size'>" +
                    "<label> 暂无数据</label>" +
                    "</div>" +
                    "</li>"
                );
            }

            //返回的数据用data.rows获取内容
            $.each(data.datas, function (i, item) {
                $companySize.append(
                    "<li>" +
                    "<div class='filter-name-item'>" +
                    "<input type='checkbox' value='" + item.value + "' name='company-size'>" +
                    "<label> " + item.value + "</label>" +
                    "</div>" +
                    "</li>"
                );
            });
        },
        error: function (err) {
            $mySelect.empty();
            $mySelect.append(
                "<li>" +
                "<div class='filter-name-item'>" +
                "<input type='checkbox' value='' name='company-size'>" +
                "<label> 暂无数据</label>" +
                "</div>" +
                "</li>"
            );
            console.log(err);
        }

    });
}

// 搜索框
function click_search() {
    search_value = $('#search').val();
    ArgsChanged();
}

//  工作地点
function click_position() {
    position_value = $('#position').val();
    ArgsChanged();
}

// 融资情况
function click_type() {
    financingSituation = $('#financingSituation .current').html();
    if (financingSituation === "不限") {
        financingSituation = "";
    }
    ArgsChanged();
}

//  公司规模
function click_company() {
    var checkbox = document.getElementsByName('company-size');
    companySize = ''
    for (var i = 0; i < checkbox.length; i++) {
        if (checkbox[i].checked)
            companySize += checkbox[i].value + ','; //如果选中，将value添加到变量s中
    }
    ArgsChanged();
}

function ArgsChanged() {
    getCompanies(1, pageSize, search_value,
        financingSituation, companySize, position_value)
}

function companyDetail(companyId){
    location.href ="/oa/company/companyDetailsPage/"+companyId;
}

function getFollowedList(){
    $.ajax({
        type: "POST",
        async: false,
        url: companyServer + "/company/getFollowedList",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        // headers: {
        //     "Content-Type": "application/json"
        // },
        data: {
            userId: userId,
        },
        success: function (data) {
            followedList = data.datas;
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}
